package com.gxa.uhome.service.impl;

import com.gxa.uhome.mapper.*;
import com.gxa.uhome.pojo.*;
import com.gxa.uhome.service.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Laiguowei
 * @date: 10:09 2021/6/9
 **/
@Service
@Transactional(rollbackFor =Exception.class)
public class UserOrderServiceImpl implements UserOrderService {
    @Resource
    UserOrderMapper userOrderMapper=null;
    @Resource
    SlipMapper slipMapper = null;
    @Resource
    OrderMapper orderMapper = null;
    @Resource
    OrderDetailMapper orderDetailMapper = null;
    @Resource
    GoodsTypeMapper goodsTypeMapper = null;
    @Override
    public Integer createUserOrder( HttpServletRequest request , UserOrder userOrder) {

        Integer id = userOrder.getUserId();
        userOrder.setUserOrderNo("20210610");
        Timestamp timestamp = Timestamp.valueOf("2020-06-10 12:20:20");
        userOrder.setRegisterTime(timestamp);
        //前端处理最少要一张照片才能提交
        String img1 = (String) request.getSession().getAttribute("1");
//        String s = null;
//        System.out.println(s.toString());

        String img2 = (String) request.getSession().getAttribute("2");

        slipMapper.insert(id,img1);

        if (img2!=null){
            Integer insert = slipMapper.insert(id, img2);
        }
        Integer userOrder1 = userOrderMapper.createUserOrder(userOrder);

        return userOrder1;
    }

    @Override
    public Integer updateUserOrder(UserOrder userOrder) {
        Integer integer = userOrderMapper.updateUserOrder(userOrder);
        return integer;
    }

    @Override
    public List<Map<String,Object>> selectByPage(Map<String, Object> map) {
        List<Map<String,Object>> userOrders = userOrderMapper.selectByPage(map);
        return userOrders;

    }

    @Override
    public UserOrder getDetail(Integer id) {
        UserOrder detail = userOrderMapper.getDetail(id);
        return detail;
    }


    @Override
    public List<UserOrder> selectByUserId(Integer id) {
        List<UserOrder> userOrders = userOrderMapper.selectByUserId(id);
        return userOrders;
    }

    @Override
    public UserOrder getDetail1(Integer id) {
        UserOrder detail= userOrderMapper.getDetail1(id);
        List<Order> orders = detail.getOrders();
        List<Slip> slips = detail.getSlips();
        User user = detail.getUser();
        Station station = detail.getStation();
        UserAddress userAddress = detail.getUserAddress();
        Map<String,Object> map = new HashMap<>();
        if (user!=null){
            map.put("user",user);
        }else if (userAddress!=null){
            map.put("adress",userAddress);
        }else if (station!=null){
            map.put("station",station);
        }else if (slips!=null){
            map.put("slips",slips);
        }else if (orders!=null){
            map.put("orders",orders);
        }
        return detail;
    }

    @Override
    public Integer selectOrderCountByStation(Integer id) {
        Integer integer = userOrderMapper.selectOrderCountByStation(id);
        return integer;
    }

    @Override
    public List<Map<Integer, Integer>> selectOrderCountByStatus(Integer id) {
        List<Map<Integer, Integer>> maps = userOrderMapper.selectOrderCountByStatus(id);
        return maps;
    }

    @Override
    public List<UserOrder> selectOrderByStatus(Integer id, Integer status) {
        List<UserOrder> userOrders = userOrderMapper.selectOrderByStatus(id, status);
        return userOrders;
    }

    @Override
    public Integer selectAllOrderCount() {
        Integer integer = userOrderMapper.selectAllOrderCount();
        return integer;
    }

    @Override
    public UserOrder getDetailByUserOrderId(Integer userOrderId) {
        UserOrder detailByUserOrderId = userOrderMapper.getDetailByUserOrderId(userOrderId);
        return detailByUserOrderId;
    }

    @Override
    @Transactional
    public Integer reviewAndupdateUserOrder(UserOrder userOrder) {
        //修改用户訂單用戶名
        userOrderMapper.updateUserOrder(userOrder);
        UserAddress userAddress = userOrder.getUserAddress();

        List<Slip> slips = userOrder.getSlips();
        for (Slip slip : slips) {
            slipMapper.update(slip);
        }
        List<Order> orders = userOrder.getOrders();
        for (Order order : orders) {
            orderMapper.createOrder(order);
            List<OrderDetail> orderDetails = order.getOrderDetails();
            for (OrderDetail orderDetail : orderDetails) {
               orderDetailMapper.createOrderDetail(orderDetail);

            }
        }

        return  1;
    }
}
